<template>
  <van-popup v-model="visible" round position="bottom" @close="handleCancel">
		<!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :span="14">
            <a-form-item label="客户姓名">
              <a-input placeholder="请输入客户姓名" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="10">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery">查询</a-button>
              <a-button type="primary" @click="searchReset" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>
    <!-- table区域-begin -->
    <div style="height:300px;overflow-y:scroll;">
      <van-list
        v-model="loading"
        :finished="finished"
        offset="30"
        finished-text="没有更多了"
        @load="downLoadData"
        :immediate-check="false">
        <div v-for="(item,i) in dataSource" class="list">
          <van-checkbox v-model="item.check"></van-checkbox>
          <div>{{ item.name }}</div>
          <div>{{ item.phone }}</div>
          <div>{{ item.address }}</div>
        </div>
      </van-list>
    </div>
    <a-button type="primary" @click="handleOk" style="display:block;margin:10px auto;">确定</a-button>
    

	</van-popup>
  <!-- <a-modal
    :title="title"
    :width="'100%'"
    :visible="visible"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    
    

  </a-modal> -->
</template>

<script>

  import {getAction,postAction} from '@/api/manage'
  import '@/assets/less/TableExpand.less'
  import {mixinDevice} from '@/utils/mixin'
  import {JeecgListMixin} from '@/mixins/JeecgListMixin'

  export default {
    name: "selectCustomerModal",
    mixins: [JeecgListMixin, mixinDevice],
    data() {
      return {
        finished: false,
        title: "",
        names: [],
        visible: false,
        placement: 'right',
        description: '',
        // 查询条件
        queryParam: {},
        //职务条件
        post:'',
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 50,
            align: "center",
            customRender: function (t, r, index) {
              return parseInt(index) + 1;
            }
          },
          {
            title: '客户姓名',
            align: "center",
            width: 100,
            dataIndex: 'name'
          },
          {
            title: '电话',
            align: "center",
            width: 100,
            dataIndex: 'phone'
          },
          {
            title: '地址',
            align: "center",
            width: 100,
            dataIndex: 'address'
          },
        ],
        //数据集
        dataSource: [],
        //工种数据
        kindOfWorkData:[],
        loading: false,

        selectedRows: [],
        url: {
          checkCustomerAddCostUrl: "/ghCustomerChooseCost/checkCustomerAddCost",
          copySchemeUrl: "/ghCustomerChooseCost/copyScheme",
        },
        currentCustomerId: 0,
        ipagination:{
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + "-" + range[1] + " 共" + total + "条"
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        },
        /* 排序参数 */
        isorter:{
          column: 'createTime',
          order: 'desc',
        },
        /* 筛选参数 */
        filters: {},
        /* table加载状态 */
      }
    },
    created() {
      //this.loadData();
    },
    methods: {
      downLoadData(){
        this.ipagination.current++;
        this.finished = false
        this.loadData()
      },
      formatListCheck(list){
        let arr = list
        for(let i=0;i<arr.length;i++){
          arr[i].check = false
        }
        return arr
      },
      filterOptionKindOfWork(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      changeKindOfWork(value){
        this.loadData(1);
      },
      searchQuery() {
        this.loadData(1);
      },
      searchReset() {
        this.queryParam = {};
        this.loadData(1);
      },
      handleCancel() {
        this.visible = false;
        this.formatListCheck(this.dataSource);
      },
      handleOk() {
        let list = this.dataSource.filter((item) => {return item.check})

        if (list.length <= 0){
          this.$message.warning("请至少选择一个客户")
          return;
        }
        this.visible = false
        let that = this;
        this.$confirm({
          title: "确认复制",
          content: "是否确定将当前方案复制给所选客户?",
          onOk: function () {
            let ids = "";
            list.forEach(function (item) {
              ids += item.id;
              ids += ",";
            })
            that.loading = true;
            getAction(that.url.copySchemeUrl, {ids:ids,currentCustomerId:that.currentCustomerId}).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.loading = false;
              that.visible = false;
              that.formatListCheck(this.dataSource)
            })
          }
        });
      },
      add(currentCustomerId) {
        this.loadData();
        this.currentCustomerId = currentCustomerId;
        this.visible = true;
      },
      loadData(arg) {
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1;
          this.dataSource = []
        }
        var params = this.getQueryParams();//查询条件
        getAction(this.url.checkCustomerAddCostUrl, params).then((res) => {
          if (res.success) {
            //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
            let list = res.result.records||res.result;
            
            this.dataSource = [...this.formatListCheck(this.dataSource), ...this.formatListCheck(list)]
            if(res.result.total){
              this.ipagination.total = res.result.total;
            }else{
              this.ipagination.total = 0;
            }
            this.loading = false
            if(list.length<10){
              this.finished = true;
            }
            //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          }else{
            this.finished = true;
            this.$message.warning(res.message)
          }
        })
      },


      onSelect(record, selected) {
        if(selected) {
          this.selectedRows.push(record);
        } else {
          const delIndex = this.selectedRows.findIndex((val) => {
            return val.id === record.id
          })
          this.selectedRows.splice(delIndex,1)
        }
      },
    }
  }
</script>

<style scoped lang="less">
.van-popup{
  padding: 20px 10px 0;
}
.ant-form-item{
  margin-bottom: 0 !important;
}
.table-page-search-submitButtons{
  margin-bottom: 0 !important;
}
  .list{
    display: flex;
    align-items: center;
    height: 40px;
    &>div{
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    &>div:nth-child(1){
      width: 10%;
    }
    &>div:nth-child(2){
      width: 15%;
    }
    &>div:nth-child(3){
      width: 35%;
    }
    &>div:nth-child(4){
      width: 40%;
    }
  }
  ::-webkit-scrollbar {
    display: none;
  }
  .ant-card-body .table-operator {
    margin-bottom: 18px;
  }

  .ant-table-tbody .ant-table-row td {
    padding-top: 15px;
    padding-bottom: 15px;
  }

  .anty-row-operator button {
    margin: 0 5px
  }

  .ant-btn-danger {
    background-color: #ffffff
  }

  .ant-modal-cust-warp {
    height: 100%
  }

  .ant-modal-cust-warp .ant-modal-body {
    height: calc(100% - 110px) !important;
    overflow-y: auto
  }

  .ant-modal-cust-warp .ant-modal-content {
    height: 90% !important;
    overflow-y: hidden
  }
</style>